/// CreateTime 2020/4/21 16:42
/// CreateUser chen 
/// ProjectName flutter_elf
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/utils/db/base_db.dart';
import 'package:sqflite/sqlite_api.dart';

class DataDB extends BaseDb{
  static const String columnId = 'id';
  static const String data = 'data';
  DataDB._internal();
  static DataDB instance = DataDB._internal();
  static DataDB getInstance()=>instance;
  @override
  String createSqlStr() {
    // TODO: implement createSql
    return '''
        create table ${getTableName()} ($columnId INTEGER primary key autoincrement,$data TEXT)
      ''';
  }

  Future<bool> isExistData(String bookId) async {
    if (bookId == null) return false;
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db
        .query(getTableName(), where: "$columnId = ?", whereArgs: [bookId]);
    return maps.isNotEmpty;
  }

  Future<DBDataResponse> getDataById(String bookId)async{
    bool _isExist = await isExistData(bookId);
    if (!_isExist) return null;

    List<DBDataResponse> books = [];
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db
        .query(getTableName(), where: "$columnId = ?", whereArgs: [bookId]);
    if (maps.isNotEmpty) {
      for (Map<String, dynamic> map in maps) {
        DBDataResponse book = DBDataResponse.fromMap(map);
        books.add(book);
      }
      return books.first;
    } else {
      return null;
    }
  }

  Future<List<DBDataResponse>> getAllData() async {
    List<DBDataResponse> books = [];
    Database db = await getDB();
    List<Map<String, dynamic>> maps = await db.query(getTableName());
    if (maps.isNotEmpty) {
      for (Map<String, dynamic> map in maps) {
        DBDataResponse book = DBDataResponse.fromMap(map);
        books.add(book);
      }
    }
    return books;
  }

  Future<int> insertOrReplaceToDB(DBDataResponse book) async {
    if (book == null) return -1;
    String id = book?.id.toString();
    if (book == null || id == null) return -1;
    bool _isExist = await isExistData(id);
    if (!_isExist) {}
    Database db = await getDB();
    Map<String, String> map = book.toMap();
    return await db.insert(getTableName(), map);
  }

  Future<bool> updateBook(DBDataResponse book) async {
    String id = book?.id.toString();
    if (book == null || id == null) return false;
    bool _isExist = await isExistData(id);
    if (!_isExist) return false;
    Database db = await getDB();
    Map<String, String> map = book.toMap();
    int result = await db
        .update(getTableName(), map, where: "$columnId = ?", whereArgs: [id]);
    return result == 1;
  }
  Future<bool> deleteBook(String bookId) async {
    bool _isExist = await isExistData(bookId);
    if (!_isExist) return false;
    Database db = await getDB();

    int result = await db
        .delete(getTableName(), where: "$columnId = ?", whereArgs: [bookId]);

    return result == 1;
  }

  clear()async{
    Database db = await getDB();
    int result = await db.delete(getTableName());
    return result == 1;
  }

  @override
  String getTableName() {
    // TODO: implement getTableName
    return this.runtimeType.toString();
  }
}

class DBDataResponse{
  String data;
  int id;
  DBDataResponse.fromMap(Map map){
    this.data = map['data'];
    this.id = map['id'];
  }
  Map<String,String> toMap(){
    return {
      "data":this.data,
      "id":this.id.toString()
    };
  }
}